package com.etoak.car.controller;

import com.alibaba.fastjson.JSONObject;
import com.etoak.car.bean.Users;
import com.etoak.car.bean.vo.ReqPageUser;
import com.etoak.car.service.UserMassageService;
import com.etoak.commons.enums.ETEnums;
import com.etoak.commons.page.Page;
import com.etoak.commons.redis.RedisUtil;
import com.etoak.commons.response.ETResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userMassage")
public class UserMessageController {
    @Autowired
    UserMassageService userMassageService;

    /**
     * 拿取用户信息
     */
    @PostMapping("/user")
    public ETResponse querySome(@RequestBody ReqPageUser reqPageUser){
        Page<Users> usersList = userMassageService.querySome(reqPageUser.getPageNum(),reqPageUser.getPageSize(),reqPageUser.getRealname());

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(usersList);
        return response;
    }
    /**
     * 删除用户信息
     */
    @PostMapping(value = "/{id}")
    public ETResponse delUser(@PathVariable int id){
        userMassageService.delUser(id);

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("成功");
        return response;

    }
    /**
     * 修改为vip
     */
    @PostMapping(value = "/updateVip")
    public ETResponse updateVip(Users users){

        if(users.getVip()==1){
            users.setVip(0);
            userMassageService.updateVip(users);
        }else {
            users.setVip(1);
            userMassageService.updateVip(users);
        }
        RedisUtil ru = RedisUtil.getRedisUtil();

        ru.setex(users.getId()+"", 1800 * 365 * 24, JSONObject.toJSONString(users));

        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("成功");
        return response;

    }

    /**
     * 充值金额
     */
    @PostMapping(value = "/addMoney")
    public ETResponse addMoney(Users users){

        userMassageService.addMoney(users);

        RedisUtil ru = RedisUtil.getRedisUtil();

        ru.setex(users.getId()+"", 1800 * 365 * 24, JSONObject.toJSONString(users));
        ETResponse response = new ETResponse();
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("成功");
        return response;

    }
}
